<template>
  <div>
    <!-- 行内表单 -->
    <el-form :inline="true" style="margin: 10px 0">
      <el-form-item>
        <el-input
          placeholder="角色名称"
          v-model="searchobj.roleName"
        ></el-input>
      </el-form-item>
      <el-button type="primary" icon="el-icon-search" @click="this.getRoleList"
        >查询</el-button
      >
      <el-button @click="empty">清空</el-button>
    </el-form>
    <!-- 添加和批量删除的按钮 -->
    <div>
      <el-button type="primary" @click="addRoles">添加</el-button>
      <el-button type="danger" @click="batchdeletion" :disabled="this.multipleSelection.length===0">批量删除</el-button>
    </div>
    <!-- 表格区域 -->
    <el-table
      :data="tableList"
      tooltip-effect="dark"
      style="width: 100%; margin: 20px 0"
      v-loading="listLoading"
      @selection-change="handleSelectionChange"
      border
      width="100%"
    >
      <el-table-column type="selection" width="55"></el-table-column>
      <el-table-column
        type="index"
        width="80"
        label="序号"
        align="center"
      ></el-table-column>
      <el-table-column
        label="角色名称"
        width="width"
        prop="roleName"
      ></el-table-column>
      <el-table-column label="操作" width="width">
        <template slot-scope="{ row, $index }">
          <el-button
            type="info"
            icon="el-icon-info"
            size="mini"
            title="分配权限"
            @click="assignpermissions(row)"
          ></el-button>
          <el-button
            type="primary"
            icon="el-icon-edit"
            size="mini"
            title="修改角色"
          ></el-button>
          <el-button
            type="danger"
            icon="el-icon-delete"
            size="mini"
            title="删除角色"
          ></el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 底部分页区域 -->
    <el-pagination
      style="margin-top: 20px; textalign: center"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="page"
      :page-sizes="[3, 5, 10]"
      :page-size="limit"
      :page-count="7"
      layout=" prev, pager, next, jumper,->,sizes, total"
      :total="total"
    >
    </el-pagination>
    <!-- 添加角色的对话框 -->
    <el-dialog
      title="添加角色"
      :visible.sync="addRolesdialogFormVisible"
      width="600px"
    >
      <el-form :model="addRolesform">
        <el-form-item label="请输入新名称" label-width="formLabelWidth">
          <el-input
            v-model="addRolesform.roleName"
            autocomplete="off"
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="addRolesdialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitRole"
          >确 定</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>
<script>
export default {
  data() {
    return {
      searchobj: {
        roleName: "",
      },
      page: 1,
      limit: 5,
      total: 0,
      tableList: [], //表格数据源
      //加载表格数据前有个loading效果
      listLoading: false,
      addRolesdialogFormVisible: false, //控制添加角色对话框的显示与隐藏
      addRolesform: {
        roleName: "",
      }, //添加角色的对话框表单
      multipleSelection:[],//表格选中项
    };
  },
  mounted() {
    this.getRoleList();
  },
  methods: {
    //获取角色列表数据
    async getRoleList() {
      //解构出三个参数
      const { page, limit, searchobj } = this;
      const result = await this.$API.role.reqRolesList(page, limit, searchobj);
      console.log(result, "获取角色列表数据");
      if (result.code == 20000) {
        this.tableList = result.data.items;
        this.total = result.data.total;
      }
    },
    //每页显示多少条数据发生变化触发的事件
    handleSizeChange(limit) {
     this.limit=limit;
     this.getRoleList();
    },
    //当前页发生变化触发的事件
    handleCurrentChange(page) {
     this.page=page;
     this.getRoleList();
    },
    //表格选中项发生变化触发的事件
    handleSelectionChange(val) {
     this.multipleSelection=val;
     

    },
    //点击清空按钮触发的事件
    empty() {
      this.searchobj.roleName = "";
      this.getRoleList();
    },
    //点击添加角色按钮触发的事件
    addRoles() {
      this.addRolesdialogFormVisible = true;
    },
    //点击添加角色对话框的确定按钮触发的事件
    async submitRole(){
     const role=this.addRolesform;
     const result=await this.$API.role.reqsaveRole(role);
     console.log(result,"111");
     if(result.code==20000){
      this.$message.success('添加角色成功');
     }
     this.addRolesdialogFormVisible=false;
     this.getRoleList();
    },
    //点击批量删除按钮触发的事件
    async batchdeletion(){
     //拿到选中的数据的id赋值给变量ids
     const ids=this.multipleSelection.map(item=>item.id)

     const result=await this.$API.role.reqremoveRoles(ids);
     console.log(result,"批量删除");
     if(result.code==200){
      this.$message.success('删除成功');
     }
     this.getRoleList();
    },
    //点击分配权限按钮触发的事件
     assignpermissions(row){
      //   @click="$router.push(`/acl/role/auth/${row.id}?roleName=${row.roleName}`)"
     this.$router.push(`/permission/role/auth/${row.id}?roleName=${row.roleName}`)
    }
  },
};
</script>
<style scoped>
</style>